package com.jhgsys.internal.base.controller;

import com.jhgsys.internal.base.entity.BaseProjectUser;
import com.jhgsys.internal.base.service.IBaseProjectUserService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目用户 Controller
 *
 * @author admin
 * @date 2024-07-29 15:56:15
 */
@Slf4j
@Validated
@Controller
public class BaseProjectUserController extends BaseController {

    @Autowired
    private IBaseProjectUserService baseProjectUserService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProjectUser")
    @RequiresPermissions("baseProjectUser:view")
    public String baseProjectUserIndex(){
        return FebsUtil.view("base/baseProjectUser/baseProjectUserList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProjectUser/add")
    @RequiresPermissions("baseProjectUser:add")
    public String baseProjectUserAdd(){
        return FebsUtil.view("base/baseProjectUser/baseProjectUserEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProjectUser/update/{id}")
    @RequiresPermissions("baseProjectUser:update")
    public String baseProjectUserUpdate(Model model, @PathVariable("id") Integer id){
        model.addAttribute("baseProjectUser", baseProjectUserService.findBaseProjectUserById(id));
        return FebsUtil.view("base/baseProjectUser/baseProjectUserEdit");
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-07-29 15:56:15
     */
    @GetMapping("baseProjectUser")
    @ResponseBody
    @RequiresPermissions("baseProjectUser:view")
    public FebsResponse getAllBaseProjectUsers(BaseProjectUser baseProjectUser) {
        return new FebsResponse().success().data(baseProjectUserService.findBaseProjectUsers(baseProjectUser));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-07-29 15:56:15
     */
    @GetMapping("baseProjectUser/list")
    @ResponseBody
    @RequiresPermissions("baseProjectUser:view")
    public FebsResponse baseProjectUserList(QueryRequest request, BaseProjectUser baseProjectUser) {
        Map<String, Object> dataTable = getDataTable(this.baseProjectUserService.findBaseProjectUsers(request, baseProjectUser));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 BaseProjectUser
     * @author admin
     * @date 2024-07-29 15:56:15
     */
    @Log("新增BaseProjectUser")
    @PostMapping("baseProjectUser")
    @ResponseBody
    @RequiresPermissions("baseProjectUser:add")
    public FebsResponse addBaseProjectUser(@Valid BaseProjectUser baseProjectUser) throws FebsException {
        try {
            baseProjectUser.setCreatedBy(getCurrentUser().getUserName());
            this.baseProjectUserService.createBaseProjectUser(baseProjectUser);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增BaseProjectUser失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 BaseProjectUser
     * @author admin
     * @date 2024-07-29 15:56:15
     */
    @Log("逻辑删除BaseProjectUser")
    @GetMapping("baseProjectUser/delete/{id}")
    @ResponseBody
    @RequiresPermissions("baseProjectUser:delete")
    public FebsResponse deleteBaseProjectUser(@PathVariable("id") String baseProjectUserIds) throws FebsException {
        try {
            BaseProjectUser baseProjectUser = new BaseProjectUser();
            baseProjectUser.setDeletedBy(getCurrentUser().getUserName());
            baseProjectUser.setDeletedTime(new Date());
            baseProjectUser.setDeletedFlg(FebsConstant.INVALID);
            this.baseProjectUserService.deleteBaseProjectUser(baseProjectUser,baseProjectUserIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseProjectUser失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 BaseProjectUser
    * @author admin
    * @date 2024-07-29 15:56:15
    */
    @Log("物理删除BaseProjectUser")
    @GetMapping("baseProjectUser/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("baseProjectUser:delete")
    public FebsResponse physicallyDeleteBaseProjectUser(@PathVariable("id") String baseProjectUserIds) throws FebsException {
        try {
            this.baseProjectUserService.physicallyDeleteBaseProjectUser(baseProjectUserIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseProjectUser失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 BaseProjectUser
     * @author admin
     * @date 2024-07-29 15:56:15
     */
    @Log("修改BaseProjectUser")
    @PostMapping("baseProjectUser/update")
    @ResponseBody
    @RequiresPermissions("baseProjectUser:update")
    public FebsResponse updateBaseProjectUser(BaseProjectUser baseProjectUser) throws FebsException {
        try {
            baseProjectUser.setUpdatedBy(getCurrentUser().getUserName());
            this.baseProjectUserService.updateBaseProjectUser(baseProjectUser);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改BaseProjectUser失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 BaseProjectUser的excel
     * @author admin
     * @date 2024-07-29 15:56:15
     */
    @PostMapping("baseProjectUser/excel")
    @ResponseBody
    @RequiresPermissions("baseProjectUser:view")
    public void export(QueryRequest queryRequest, BaseProjectUser baseProjectUser, HttpServletResponse response) throws FebsException {
        try {
            List<BaseProjectUser> baseProjectUsers = this.baseProjectUserService.findBaseProjectUsers(queryRequest, baseProjectUser).getRecords();
            ExcelKit.$Export(BaseProjectUser.class, response).downXlsx(baseProjectUsers, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
